package com.prestamomo.vil.ppa.pmvilmedium.pmvilview;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Point;
import android.util.AttributeSet;
import android.util.Log;
import android.util.TypedValue;
import android.view.MotionEvent;
import android.view.View;

import com.prestamomo.vil.ppa.R;

import java.util.HashMap;
import java.util.Map;


public class PmvilHorizontalSelector extends View {

    private static final String Pmvil_TAG = "HorizontalSelector";

    private Context Pmvil_mContext;
    private PmvilOnSelectChangeListener Pmvil_mListener;

    private int Pmvil_mMinValue;
    private int Pmvil_mMaxValue;
    private int Pmvil_mCurrentPosition;
    private int Pmvil_mItemCount;
    private float Pmvil_mItemWidth;

    private Paint Pmvil_mPaint;


    private Paint Pmvil_bgPaint;
    private Bitmap Pmvil_bitmap;
    private Paint Pmvil_checkPaint;
    private int Pmvil_checkPosition = 0;
    private int Pmvil_changePosition = 0;

    public PmvilHorizontalSelector(Context Pmvil_context) {
        super(Pmvil_context);
        PmvilInitV(Pmvil_context);
    }

    public PmvilHorizontalSelector(Context Pmvil_context, AttributeSet Pmvil_attrs) {
        super(Pmvil_context, Pmvil_attrs);
        PmvilInitV(Pmvil_context);
    }

    public PmvilHorizontalSelector(Context pmvil_context, AttributeSet pmvil_attrs, int pmvil_defStyleAttr) {
        super(pmvil_context, pmvil_attrs, pmvil_defStyleAttr);
        PmvilInitV(pmvil_context);
    }

    private void PmvilInitV(Context pmvilContext) {
        Pmvil_mContext = pmvilContext;
        Pmvil_mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        Pmvil_mPaint.setStyle(Paint.Style.FILL);
        Pmvil_mPaint.setColor(Color.BLACK);

        Pmvil_bgPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        Pmvil_bgPaint.setStyle(Paint.Style.FILL);
        Pmvil_bgPaint.setColor(Color.parseColor("#FFCE8F"));
        Pmvil_bitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.free_pmvil_icon);

        Pmvil_checkPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        Pmvil_checkPaint.setStyle(Paint.Style.FILL);
        Pmvil_checkPaint.setColor(Color.parseColor("#FF7C32"));
    }

    public void setPmvilRange(int pmvil_minValue, int pmvil_maxValue, int pmvil_minCheckNum) {
        Pmvil_mMinValue = pmvil_minValue;
        Pmvil_mMaxValue = pmvil_maxValue;
        Pmvil_mItemCount = pmvil_maxValue - pmvil_minValue + 1;
        Pmvil_mCurrentPosition = 0;
        Pmvil_checkPosition = pmvil_minCheckNum;
        Pmvil_changePosition = Pmvil_checkPosition;
        setPmvilPosition(pmvil_minCheckNum);

    }

    public void setPmvilPosition(int pmvil_position) {
        Map<String,Object> pmvilPostion = new HashMap<>();
        if (pmvil_position >= 0 && pmvil_position < Pmvil_mItemCount) {
            Pmvil_mCurrentPosition = pmvil_position;
            pmvilPostion.put("pmvilsion",Pmvil_mCurrentPosition);
            if (Pmvil_mListener != null) {
                Pmvil_mListener.onPmvilSelectChange(Pmvil_mCurrentPosition);
                pmvilPostion.put("pmvilsion",Pmvil_mCurrentPosition);
            }
            invalidate();
        } else {
            pmvilPostion.put("pmvilsion",0);
            Log.w(Pmvil_TAG, "setPosition: position out of range");
        }
        if(pmvilPostion.size()!=0) pmvilPostion.clear();
    }
    public void setPmvilPositionChangeView(int pmvilChangePosition) {
        if (pmvilChangePosition >= 0 && pmvilChangePosition < Pmvil_mItemCount) {
            invalidate();
        } else {
            Log.w(Pmvil_TAG, "setPosition: position out of range");
        }
    }

    public void setPmvilOnSelectChangeListener(PmvilOnSelectChangeListener pmvilListener) {
        Pmvil_mListener = pmvilListener;
    }

    float pmvilChagePost = 0;
    @Override
    protected void onDraw(Canvas pmvilCanvas) {
        super.onDraw(pmvilCanvas);

        Pmvil_mItemWidth = (float) getWidth() / Pmvil_mItemCount;
        pmvilChagePost = Pmvil_changePosition * Pmvil_mItemWidth - Pmvil_bitmap.getWidth()/2;

        pmvilCanvas.drawRoundRect(0, pmvilDp2px(5), getWidth(), getHeight() - pmvilDp2px(5), 20, 20, Pmvil_bgPaint);

        for (int i = 0; i < Pmvil_mItemCount; i++) {
            if (i != 0) {
                if(pmvilChagePost >0&&i == Pmvil_changePosition){
                    pmvilCanvas.drawRoundRect(i * Pmvil_mItemWidth - pmvilDp2px(2), 0, i * Pmvil_mItemWidth + pmvilDp2px(2), pmvilDp2px(5), 10, 10, Pmvil_checkPaint);
                    pmvilCanvas.drawRoundRect(i * Pmvil_mItemWidth - pmvilDp2px(2), getHeight() - pmvilDp2px(5), i * Pmvil_mItemWidth +  pmvilDp2px(2), getHeight(), 10, 10, Pmvil_checkPaint);
                }else {
                    pmvilCanvas.drawRoundRect(i * Pmvil_mItemWidth - pmvilDp2px(2), 0, i * Pmvil_mItemWidth + pmvilDp2px(2), pmvilDp2px(5), 10, 10, Pmvil_bgPaint);
                    pmvilCanvas.drawRoundRect(i * Pmvil_mItemWidth - pmvilDp2px(2), getHeight() - pmvilDp2px(5), i * Pmvil_mItemWidth +  pmvilDp2px(2), getHeight(), 10, 10, Pmvil_bgPaint);
                }
            }
        }

        if(pmvilChagePost >0){
            pmvilCanvas.drawRoundRect(pmvilDp2px(2), pmvilDp2px(7), pmvilChagePost + Pmvil_bitmap.getWidth()/2, getHeight() - pmvilDp2px(7), 20, 20, Pmvil_checkPaint);
        }

        Matrix pmvilMatrix = new Matrix();
        // compute init left, top
        int pmvilBbw = Pmvil_bitmap.getWidth();
        int pmvilBbh = Pmvil_bitmap.getHeight();
        Point pmvilCenter = new Point(getWidth() / 2, getHeight() / 2);
        Point pmvilBmpCenter = new Point(pmvilBbw / 2, pmvilBbh / 2);
        pmvilMatrix.postTranslate(pmvilChagePost, pmvilCenter.y - pmvilBmpCenter.y);
        pmvilCanvas.drawBitmap(Pmvil_bitmap,pmvilMatrix, Pmvil_bgPaint);
    }

    @Override
    public boolean onTouchEvent(MotionEvent pmvilEvent) {
        int action = pmvilEvent.getAction();
        if (action == MotionEvent.ACTION_DOWN) {
            return true;
        } else if (action == MotionEvent.ACTION_MOVE) {
            float x = pmvilEvent.getX();
            Pmvil_changePosition = (int) (x / Pmvil_mItemWidth);
            pmvilChagePost = Pmvil_changePosition * Pmvil_mItemWidth - Pmvil_bitmap.getWidth() / 3 * 2;
            setPmvilPositionChangeView(Pmvil_changePosition);
            return true;
        } else if (action == MotionEvent.ACTION_UP) {
            if (Pmvil_checkPosition > Pmvil_changePosition) {
                Pmvil_changePosition = Pmvil_checkPosition;
            }
            pmvilChagePost = Pmvil_changePosition * Pmvil_mItemWidth - Pmvil_bitmap.getWidth() / 3 * 2;
            setPmvilPosition(Pmvil_changePosition);
            return true;
        }
        return super.onTouchEvent(pmvilEvent);
    }

    public interface PmvilOnSelectChangeListener {
        void onPmvilSelectChange(int pmvilValue);
    }


    public int pmvilDp2px(float pmvil_dp) {
        return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, pmvil_dp,
                getResources().getDisplayMetrics());
    }
}

